//
//  RDiscoverViewController.m
//  iMoive
//
//  Created by 侯伟 on 2021/8/24.
//

#import "RDiscoverViewController.h"
#import "RDiscoverPageViewController.h"

#import "IFMMenu.h"
#import "RSSwapButton.h"

@interface RDiscoverViewController (){
    NSArray* _pageInfos;
}

@property(nonatomic,strong)UIButton* menuBtn;

@end

@implementation RDiscoverViewController


- (instancetype)init{
    if(self = [super init]){
        
        [self initPageInfo];
        
        self.titleColorSelected = COLOR_WHITE;
        self.titleFontSelected = FONTBOLD_FIT(15);
        self.titleColorNormal = COLOR_WHITE;
        self.titleFontNormal = FONT_FIT(15);
        self.menuViewStyle = WMMenuViewStyleLine;
        self.progressColor = UIColor.whiteColor;
        self.progressWidth = PX(30);
        self.progressHeight = PX(5.0);
        self.menuItemWidth = (SCREEN_WIDTH - PX(71))/_pageInfos.count;
        self.menuViewLayoutMode = WMMenuViewLayoutModeScatter;
        self.preloadPolicy = WMPageControllerPreloadPolicyNeighbour;
        self.showOnNavigationBar = NO;
    }
    return self;
}

- (void)initPageInfo{
    
    NSMutableArray* pages = [NSMutableArray array];
    
    [pages addObject:@{@"type":@(VideoType_Movie),@"title":@"电影"}];
    [pages addObject:@{@"type":@(VideoType_TV),@"title":@"电视剧"}];
    [pages addObject:@{@"type":@(VideoType_Variety),@"title":@"综艺"}];
    [pages addObject:@{@"type":@(VideoType_Anime),@"title":@"动漫"}];
    
    _pageInfos = pages;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViews];
}


- (void)setupViews{
    
    self.navigationItem.title = @"观影排行";
    
    UIView* rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,PX(71), PX(44))];
    
    RSSwapButton* menuBtn = [[RSSwapButton alloc] initWithFrame:CGRectMake(0, (PX(44)-PX(20))/2.0, PX(56), PX(20))];
    menuBtn.gap = 4;
    menuBtn.backgroundColor = UIColor.whiteColor;
    [menuBtn setTitle:@"今日" forState:UIControlStateNormal];
    [menuBtn setTitleColor:COLOR_BLACK_DARK forState:UIControlStateNormal];
    [menuBtn setImage:[UIImage imageNamed:@"common_menu_down"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(timeFilter:) forControlEvents:UIControlEventTouchUpInside];
    menuBtn.titleLabel.font = FONT_FIT(11);
    
    menuBtn.layer.cornerRadius = PX(10);
    menuBtn.layer.masksToBounds = YES;
    
    [rightView addSubview:menuBtn];
    _menuBtn = menuBtn;
    
    self.menuView.rightView = rightView;
    self.menuView.backgroundColor = COLOR_THEME;
}

- (void)timeFilter:(UIButton*)sender{
    
    [UIView animateWithDuration:0.15 animations:^{
        self.menuBtn.imageView.transform =  CGAffineTransformMakeRotation(-M_PI);
    }];
 
    NSInteger selectIndex = 0;
    RDiscoverPageViewController* discoverController = (RDiscoverPageViewController*)self.currentViewController;
    if(discoverController.timeRange == TimeRange_Today){
        selectIndex = 1;
    }else if (discoverController.timeRange == TimeRange_Week){
        selectIndex = 2;
    }else if (discoverController.timeRange == TimeRange_Month){
        selectIndex = 3;
    }
    
    IFMMenuItem* item0 = [IFMMenuItem itemWithImage:NULL title:@"今日" action:^(IFMMenuItem *item) {
        [self changedTimeRange:item.index];
    }];
    item0.index = TimeRange_Today;
    
    IFMMenuItem* item1 = [IFMMenuItem itemWithImage:NULL title:@"本周" action:^(IFMMenuItem *item) {
        [self changedTimeRange:item.index];
    }];
    item1.index = TimeRange_Week;
    
    IFMMenuItem* item2 = [IFMMenuItem itemWithImage:NULL title:@"本月" action:^(IFMMenuItem *item) {
        [self changedTimeRange:item.index];
    }];
    item2.index = TimeRange_Month;
    
    IFMMenu *menu = [[IFMMenu alloc] initWithItems:@[item0,item1,item2]];
    menu.titleColor = COLOR_BLACK_DARK;
    menu.titleFont = FONT_FIT(12);
    menu.menuBackGroundColor = UIColor.whiteColor;
    menu.segmenteLineColor = COLOR_GRAY_BASE;
    menu.minMenuItemHeight = PX(32);
    menu.minMenuItemWidth = PX(60);
    menu.gapBetweenImageTitle = 0;
    menu.arrowHight = 0.f;
    menu.edgeInsets = UIEdgeInsetsMake(0, 20, 0, 20);
    menu.menuBackgroundStyle = IFMMenuBackgroundStyleLight;
    menu.containerViewBackGroundColor = UIColor.clearColor;
    menu.menuCornerRadiu = PX(10.0);
    menu.showShadow = YES;
    menu.selectedIndex = selectIndex;
    menu.selectedColor = COLOR_THEME;
    menu.dismissBlock = ^{
        [UIView animateWithDuration:0.15 animations:^{
            self.menuBtn.imageView.transform = CGAffineTransformIdentity;
        }];
    };
    
    CGRect rect = [sender.superview convertRect:sender.frame toView:self.tabBarController.view];
    rect.origin.y += 6.0;
    [menu showFromRect:rect inView:self.tabBarController.view];
}

- (void)changedTimeRange:(TimeRange)timeRange{
    
    NSString* menuTitle = @"今日";
    if(timeRange == TimeRange_Today){
        menuTitle = @"今日";
    }else if (timeRange == TimeRange_Week){
        menuTitle = @"本周";
    }else if (timeRange == TimeRange_Month){
        menuTitle = @"本月";
    }
    [self.menuBtn setTitle:menuTitle forState:UIControlStateNormal];
    
    RDiscoverPageViewController* discoverController = (RDiscoverPageViewController*)self.currentViewController;
    [discoverController reloadDataWithTimeRange:timeRange];
}

#pragma mark -- WMPageControllerDataSource WMPageControllerDelegate
//WMPageControllerDelegate

- (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    
    NSString* menuTitle = @"今日";
    RDiscoverPageViewController* discoverController = (RDiscoverPageViewController*)viewController;
    if(discoverController.timeRange == TimeRange_Today){
        menuTitle = @"今日";
    }else if (discoverController.timeRange == TimeRange_Week){
        menuTitle = @"本周";
    }else if (discoverController.timeRange == TimeRange_Month){
        menuTitle = @"本月";
    }
    [self.menuBtn setTitle:menuTitle forState:UIControlStateNormal];
}

//WMPageControllerDataSource
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return _pageInfos.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    
    NSDictionary* pageInfo = _pageInfos[index];
    return pageInfo[@"title"];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    
    NSDictionary* pageInfo = _pageInfos[index];
    VideoType VideoType = [pageInfo[@"type"] integerValue];
    
    RDiscoverPageViewController* discoverController = [RDiscoverPageViewController new];
    discoverController.VideoType = VideoType;
    discoverController.timeRange = TimeRange_Today;
    return discoverController;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    CGFloat y = 0;
    return CGRectMake(0, y, self.view.frame.size.width, PX(44));
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, SCREEN_HEIGHT - originY-CGRectGetHeight(self.tabBarController.tabBar.frame));
}
@end
